package com.english_test_sys.service.Impl;

import com.english_test_sys.mapper.UserMapper;
import com.english_test_sys.pojo.User;
import com.english_test_sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        User user = userMapper.findByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }

    @Override
    public void register(User user) {
        userMapper.insert(user);
    }

    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public User updateScoreByUsername(String username, Integer newScore) {
        User user = userMapper.findByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        // 更新分数
        user.setScore(user.getScore() + newScore);

        userMapper.updateScoreByUsername(username, newScore);
        return user;
    }
}
